import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by enzoism on 2019/6/9.
 */
public class CountDownLatchRunable implements Runnable {
    private CountDownLatch countDownLatch;
    public CountDownLatchRunable(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }
    @Override
    public void run() {
        try {
            Thread.sleep(Math.abs(new Random().nextInt() % 10000));
            System.out.println("--------->>>"+Thread.currentThread().getName() + "任务检查完成");
            countDownLatch.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final int threadCount = 10;
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        ExecutorService exec = Executors.newFixedThreadPool(threadCount);
        CountDownLatchRunable runable = null;
        for (int i = 0; i < threadCount; i++) {
            runable = new CountDownLatchRunable(countDownLatch);
            exec.submit(runable);
        }
        // 等待所有线程检查完成
        countDownLatch.await();
        // 执行主程序
        System.out.println("--->火箭发射");
        // 线程池退出
        exec.shutdown();

    }
}
